﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.AppVersion;
using FwProject.ShoppingMall.Com;

namespace FwProject.Manager
{
    public class ShopReceivingAddressManager : IDomainService
    {
        private readonly IRepository<ShopReceivingAddress, Guid> _shopReceivingAddressRepository;

        public ShopReceivingAddressManager(IRepository<ShopReceivingAddress, Guid> shopReceivingAddressRepository)
        {
            _shopReceivingAddressRepository = shopReceivingAddressRepository;
        }
        /// <summary>
        /// 添加一个新地址
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> CreateShopReceivingAddress(ShopReceivingAddress model)
        {
            if (model.AreaId.Length == 0 && model.AreaId.Length > 10)
                new UserFriendlyException("区域选择错误");

            var m = _shopReceivingAddressRepository.GetAll().Where(o => o.UserId == model.UserId && o.DefalutAddress == true);


            if (m.Count() > 0)
            {
                if (model.DefalutAddress == true)
                {
                    foreach (var item in m)
                    {
                        item.DefalutAddress = false;
                    }
                }
            }
            else {
                model.DefalutAddress = true;
            }
            return await _shopReceivingAddressRepository.InsertAndGetIdAsync(model);
        }
        /// <summary>
        /// 修改样一个收货地址
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task UpdateShopReceivingAddress(ShopReceivingAddress model)
        {
            if (model.AreaId.Length == 0 && model.AreaId.Length > 10)
                new UserFriendlyException("区域选择错误");

            var m = _shopReceivingAddressRepository.GetAll().Where(o => o.UserId == model.UserId && o.DefalutAddress == true);
            if (m.Count() > 0)
            {
                if (model.DefalutAddress == true)
                {
                    foreach (var item in m)
                    {
                        item.DefalutAddress = false;
                    }
                }
             } else
            {
                model.DefalutAddress = true;
            }


            await _shopReceivingAddressRepository.UpdateAsync(model);
        }

        /// <summary>
        /// 删除一个收货地址
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task DeleteShopReceivingAddress(ShopReceivingAddress model)
        {
            await _shopReceivingAddressRepository.DeleteAsync(model);
        }
        /// <summary>
        /// 设置为默认地址
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public void SetDefalutAddress(Guid Id)
        {
            var modelList=_shopReceivingAddressRepository.GetAll();
            foreach (var item in modelList)
            {
                if (item.Id == Id)
                { item.DefalutAddress = true; }
                else {
                    item.DefalutAddress = false;
                }
            }

        }
    }
}
